package com.kye.express.business.invoice_online;

import android.content.Context;
import android.text.TextUtils;

import com.kye.express.R;
import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.RxView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.base.RetrofitManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.invoice_online.GeneratedInvoiceDataResultBean;
import com.kye.model.bean.invoice_online.GetInvoicedListResultBean;
import com.kye.model.bean.invoice_online.RequestFindWaybillAmountBean;
import com.kye.model.bean.invoice_online.RequestGeneratedInvoiceDataBean;
import com.kye.model.bean.invoice_online.RequestGetInvoicedListBean;
import com.kye.model.bean.user.User;
import com.kye.model.constant.Constants;
import com.kye.model.service.InvoiceOnlineService;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public interface InvoiceOnlineActivityContract {

    interface InvoiceOnlineActivityView extends RxView {

        void onComplete(boolean isLoadMore);

        void onLoading();

        void onLoaded(List<GetInvoicedListResultBean> datas, boolean isLoadMore);

        void onError(String msg, boolean isLoadMore);

        void onOtherError(String msg);

        void onCompleteFindWaybillAmount();

        void onLoadedFindWaybillAmount(List<GetInvoicedListResultBean> datas);

        void onLoadedGeneratedData(List<GeneratedInvoiceDataResultBean> datas);

        void onErrorFindWaybillAmount(String msg);

        void onErrorGeneratedData(String msg);
    }

    class InvoiceOnlineActivityViewPresenter extends BasePresenter<InvoiceOnlineActivityContract.InvoiceOnlineActivityView> {

        private Context mContext;
        private int mCurrentPage = 1;

        public InvoiceOnlineActivityViewPresenter(Context context) {
            this.mContext = context;
        }

        /**
         * 查询运费
         */
        public void getInvoicedListData(final boolean isFirstIn, final boolean isLoadMore) {
            String phone = "";
            User user = UserSession.getInstance().getUser();
            if (user != null) {
                phone = user.getPhone();
            }

            RetrofitManager.getInstance().createOpenApiService(InvoiceOnlineService.class)
                    .getInvoicedListData(new RequestGetInvoicedListBean(phone, isLoadMore ? (mCurrentPage + 1) : 1, Constants.PAGE_SIZE))
                    .compose(getView().<OpenApiResult<List<GetInvoicedListResultBean>>>bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<List<GetInvoicedListResultBean>>() {

                        @Override
                        public void onLoading(Disposable d) {
                            if (isFirstIn) {
                                getView().onLoading();
                            }
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onError(mContext.getString(R.string.common_net_error), isLoadMore);
                        }

                        @Override
                        public void onLoaded() {
                            if (getView() != null) {
                                getView().onComplete(isLoadMore);
                            }
                        }

                        @Override
                        public void onSuccess(OpenApiResult<List<GetInvoicedListResultBean>> result, List<GetInvoicedListResultBean> datas) {
                            if (!isLoadMore) {
                                resetPage();
                            }
                            if (datas != null && datas.size() > 0) {
                                getView().onLoaded(datas, isLoadMore);
                                if (isLoadMore) {
                                    setCurrentPage(mCurrentPage + 1);
                                }
                            } else {
                                getView().onError(mContext.getString(R.string.common_no_more_datas), isLoadMore);
                            }
                        }

                        @Override
                        public void onApiError(OpenApiResult<List<GetInvoicedListResultBean>> result, int code, String msg) {
                            if (!TextUtils.isEmpty(msg)) {
                                getView().onError(msg, isLoadMore);
                            } else {
                                getView().onError(mContext.getString(R.string.common_net_error), isLoadMore);
                            }
                        }

                    });
        }

        public int getCurrentPage() {
            return mCurrentPage;
        }

        public void setCurrentPage(int currentPage) {
            this.mCurrentPage = currentPage;
        }

        public void resetPage() {
            setCurrentPage(1);
        }


        /**
         * 查询运费
         */
        public void findWaybillAmount(String waybill) {
            String phone = "";
            User user = UserSession.getInstance().getUser();
            if (user != null) {
                phone = user.getPhone();
            }

            RetrofitManager.getInstance().createOpenApiService(InvoiceOnlineService.class)
                    .findWaybillAmount(new RequestFindWaybillAmountBean(phone, waybill))
                    .compose(getView().<OpenApiResult<List<GetInvoicedListResultBean>>>bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<List<GetInvoicedListResultBean>>() {

                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onOtherError(mContext.getString(R.string.common_net_error));
                        }

                        @Override
                        public void onLoaded() {
                            if (getView() != null) {
                                getView().onCompleteFindWaybillAmount();
                            }
                        }

                        @Override
                        public void onSuccess(OpenApiResult<List<GetInvoicedListResultBean>> result, List<GetInvoicedListResultBean> datas) {
                            if (datas != null && datas.size() > 0) {
                                getView().onLoadedFindWaybillAmount(datas);
                            }else{
                                getView().onOtherError(mContext.getString(R.string.common_no_datas));
                            }
                        }

                        @Override
                        public void onApiError(OpenApiResult<List<GetInvoicedListResultBean>> result, int code, String msg) {
                            if (!TextUtils.isEmpty(msg)) {
                                getView().onErrorFindWaybillAmount(msg);
                            } else {
                                getView().onErrorFindWaybillAmount(mContext.getString(R.string.common_net_error));
                            }
                        }

                    });
        }

        /**
         * 发票生成
         */
        public void generatedData(List<GetInvoicedListResultBean> checkedInvoicList) {
            String phone = "";
            List<RequestGeneratedInvoiceDataBean.DataBean> dataBeans = new ArrayList<>();
            User user = UserSession.getInstance().getUser();
            if (user != null) {
                phone = user.getPhone();
            }
            if (checkedInvoicList != null) {
                for (GetInvoicedListResultBean item : checkedInvoicList) {
                    dataBeans.add(new RequestGeneratedInvoiceDataBean.DataBean(item.getMoney(), item.getWaybill()));
                }
            }

            RetrofitManager.getInstance().createOpenApiService(InvoiceOnlineService.class)
                    .generatedData(new RequestGeneratedInvoiceDataBean(phone, dataBeans))
                    .compose(getView().<OpenApiResult<List<GeneratedInvoiceDataResultBean>>>bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<List<GeneratedInvoiceDataResultBean>>() {

                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onOtherError(mContext.getString(R.string.common_net_error));
                        }

                        @Override
                        public void onLoaded() {
                            if (getView() != null) {
                                getView().onCompleteFindWaybillAmount();
                            }
                        }

                        @Override
                        public void onSuccess(OpenApiResult<List<GeneratedInvoiceDataResultBean>> result, List<GeneratedInvoiceDataResultBean> datas) {
                            if (datas != null) {
                                getView().onLoadedGeneratedData(datas);
                            }else{
                                getView().onOtherError(mContext.getString(R.string.invoice_generate_failed));
                            }
                        }

                        @Override
                        public void onApiError(OpenApiResult<List<GeneratedInvoiceDataResultBean>> result, int code, String msg) {
                            if (!TextUtils.isEmpty(msg)) {
                                getView().onErrorGeneratedData(msg);
                            } else {
                                getView().onOtherError(mContext.getString(R.string.common_net_error));
                            }
                        }

                    });
        }
    }
}
